angular.module('crawlerApp')
  .controller('taskSetCtr', ['$scope', 'ngDialog', 'taskHelper','toastr', function(
    $scope, ngDialog, taskHelper,toastr) {
    $scope.controllerName = 'taskSetCtr';
    $scope.dialogTitle = '添加周期任务';

    // $scope.group = {
    //   'name': '任务1',
    //   'priority': 'hightest',
    //   'setTime': {
    //     'month': ['1'
    // //     {
    // //   'name': '一月份',
    // //   'value': '1'
    // // }
    // ],
    //     'week': '1',
    //     'date': '30',
    //     'hour': '23'
    //   },
    //   'groups': []
    // }
    $scope.tasks = [];
    $scope.task = {
      "previous": "1",
      'group': [],
      "time": {
        "month": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
        "day": [0, 1, 2, 5],
        "date": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
          17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
        ],
        "hour": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
          17, 18, 19, 20, 21, 22, 23
        ]
      },
      "projects": [{
        "previous": "1",
        "name": "project1",
        "groupID": "group1",
        "depth": -1,
        "start_url": "https://segmentfault.com/a/1190000002691861",
        "rule_store": [{
          "url_pattern": [
            "https://segmentfault.com.*?"
          ],
          "link_parse_rule": [{
            "mode": "reg",
            "expression": "href=\"(https://github.com/totorojs/totoro)\""
          }],
          "content_parse_rule": {},
          "request_options": {},
          "saveAddress": "/Users/lawwy/Work/project/wf-spider/lib/downloader/html/"
        }]
      }]
    }


    $scope.groups = [{
      'groupID': 'group1',
      'projects': [{
        "previous": "1",
        "name": "project1",
        "groupID": "group1",
        "depth": -1,
        "start_url": "https://segmentfault.com/a/1190000002691861",
        "rule_store": [{
          "url_pattern": [
            "https://segmentfault.com.*?"
          ],
          "link_parse_rule": [{
            "mode": "reg",
            "expression": "href=\"(https://github.com/totorojs/totoro)\""
          }],
          "content_parse_rule": {},
          "request_options": {},
          "saveAddress": "/Users/lawwy/Work/project/wf-spider/lib/downloader/html/"
        }]
      }, {
        "previous": "1",
        "name": "project2",
        "groupID": "group1",
        "depth": -1,
        "start_url": "https://segmentfault.com/a/1190000002691861",
        "rule_store": [{
          "url_pattern": [
            "https://segmentfault.com.*?"
          ],
          "link_parse_rule": [{
            "mode": "reg",
            "expression": "href=\"(https://github.com/totorojs/totoro)\""
          }],
          "content_parse_rule": {},
          "request_options": {},
          "saveAddress": "/Users/lawwy/Work/project/wf-spider/lib/downloader/html/"
        }]
      }, {
        "previous": "1",
        "name": "project3",
        "groupID": "group1",
        "depth": -1,
        "start_url": "https://segmentfault.com/a/1190000002691861",
        "rule_store": [{
          "url_pattern": [
            "https://segmentfault.com.*?"
          ],
          "link_parse_rule": [{
            "mode": "reg",
            "expression": "href=\"(https://github.com/totorojs/totoro)\""
          }],
          "content_parse_rule": {},
          "request_options": {},
          "saveAddress": "/Users/lawwy/Work/project/wf-spider/lib/downloader/html/"
        }]
      }]
    }, {
      'groupID': 'group2',
      'projects': [{
        "previous": "1",
        "name": "project1",
        "groupID": "group2",
        "depth": -1,
        "start_url": "https://segmentfault.com/a/1190000002691861",
        "rule_store": [{
          "url_pattern": [
            "https://segmentfault.com.*?"
          ],
          "link_parse_rule": [{
            "mode": "reg",
            "expression": "href=\"(https://github.com/totorojs/totoro)\""
          }],
          "content_parse_rule": {},
          "request_options": {},
          "saveAddress": "/Users/lawwy/Work/project/wf-spider/lib/downloader/html/"
        }]
      }, {
        "previous": "1",
        "name": "project2",
        "groupID": "group2",
        "depth": -1,
        "start_url": "https://segmentfault.com/a/1190000002691861",
        "rule_store": [{
          "url_pattern": [
            "https://segmentfault.com.*?"
          ],
          "link_parse_rule": [{
            "mode": "reg",
            "expression": "href=\"(https://github.com/totorojs/totoro)\""
          }],
          "content_parse_rule": {},
          "request_options": {},
          "saveAddress": "/Users/lawwy/Work/project/wf-spider/lib/downloader/html/"
        }]
      }, {
        "previous": "1",
        "name": "project3",
        "groupID": "group2",
        "depth": -1,
        "start_url": "https://segmentfault.com/a/1190000002691861",
        "rule_store": [{
          "url_pattern": [
            "https://segmentfault.com.*?"
          ],
          "link_parse_rule": [{
            "mode": "reg",
            "expression": "href=\"(https://github.com/totorojs/totoro)\""
          }],
          "content_parse_rule": {},
          "request_options": {},
          "saveAddress": "/Users/lawwy/Work/project/wf-spider/lib/downloader/html/"
        }]
      }]
    }]

    $scope.priorities = [{
      'name': '最高',
      'value': '0'
    }, {
      'name': '正常',
      'value': '1'
    }, {
      'name': '最低',
      'value': '2'
    }];

    $scope.months = [
    {
      'name':'每月',
      'value':-1
    },
    {
      'name': '一月份',
      'value': 1
    }, {
      'name': '二月份',
      'value': 2
    }, {
      'name': '三月份',
      'value': 3
    }, {
      'name': '四月份',
      'value': 4
    }, {
      'name': '五月份',
      'value': 5
    }, {
      'name': '六月份',
      'value': 6
    }, {
      'name': '七月份',
      'value': 7
    }, {
      'name': '八月份',
      'value': 8
    }, {
      'name': '九月份',
      'value': 9
    }, {
      'name': '十月份',
      'value': 10
    }, {
      'name': '十一月份',
      'value': 11
    }, {
      'name': '十二月份',
      'value': 12
    }];

    $scope.weeks = [
{
      'name': '每天',
      'value': -2
    },
    {
      'name': '星期一',
      'value': 1
    }, {
      'name': '星期二',
      'value': 2
    }, {
      'name': '星期三',
      'value': 3
    }, {
      'name': '星期四',
      'value': 4
    }, {
      'name': '星期五',
      'value': 5
    }, {
      'name': '星期六',
      'value': 6
    }, {
      'name': '星期日',
      'value': 0
    }];
    $scope.days = [
    {
      'name':'每天',
      'value':-3
    },
    {
      'name': '1号',
      'value': 1
    }, {
      'name': '2号',
      'value': 2
    }, {
      'name': '3号',
      'value': 3
    }, {
      'name': '4号',
      'value': 4
    }, {
      'name': '5号',
      'value': 5
    }, {
      'name': '6号',
      'value': 6
    }, {
      'name': '7号',
      'value': 7
    }, {
      'name': '8号',
      'value': 8
    }, {
      'name': '9号',
      'value': 9
    }, {
      'name': '10号',
      'value': 10
    }, {
      'name': '11号',
      'value': 11
    }, {
      'name': '12号',
      'value': 12
    }, {
      'name': '13号',
      'value': 13
    }, {
      'name': '14号',
      'value': 14
    }, {
      'name': '15号',
      'value': 15
    }, {
      'name': '16号',
      'value': 16
    }, {
      'name': '17号',
      'value': 17
    }, {
      'name': '18号',
      'value': 18
    }, {
      'name': '19号',
      'value': 19
    }, {
      'name': '20号',
      'value': 20
    }, {
      'name': '21号',
      'value': 21
    }, {
      'name': '22号',
      'value': 22
    }, {
      'name': '23号',
      'value': 23
    }, {
      'name': '24号',
      'value': 24
    }, {
      'name': '25号',
      'value': 25
    }, {
      'name': '26号',
      'value': 26
    }, {
      'name': '27号',
      'value': 27
    }, {
      'name': '28号',
      'value': 28
    }, {
      'name': '29号',
      'value': 29
    }, {
      'name': '30号',
      'value': 30
    }, {
      'name': '31号',
      'value': 31
    }];

    $scope.hours = [
    {
      'name':'每小时',
      'value':-4
    },
    {
      'name': '00:00',
      'value': 0
    }, {
      'name': '01:00',
      'value': 1
    }, {
      'name': '02:00',
      'value': 2
    }, {
      'name': '03:00',
      'value': 3
    }, {
      'name': '04:00',
      'value': 4
    }, {
      'name': '05:00',
      'value': 5
    }, {
      'name': '06:00',
      'value': 6
    }, {
      'name': '07:00',
      'value': 7
    }, {
      'name': '08:00',
      'value': 8
    }, {
      'name': '09:00',
      'value': 9
    }, {
      'name': '10:00',
      'value': 10
    }, {
      'name': '11:00',
      'value': 11
    }, {
      'name': '12:00',
      'value': 12
    }, {
      'name': '13:00',
      'value': 13
    }, {
      'name': '14:00',
      'value': 14
    }, {
      'name': '15:00',
      'value': 15
    }, {
      'name': '16:00',
      'value': 16
    }, {
      'name': '17:00',
      'value': 17
    }, {
      'name': '18:00',
      'value': 18
    }, {
      'name': '19:00',
      'value': 19
    }, {
      'name': '20:00',
      'value': 20
    }, {
      'name': '21:00',
      'value': 21
    }, {
      'name': '22:00',
      'value': 22
    }, {
      'name': '23:00',
      'value': 23
    }];

    //获取周期任务列表
    $scope.getTaskList = function () {
      taskHelper.taskList().then(function (data) {
        // alert('11')
        // console.log(data,'data')
        if (data.timerList) {
          $scope.tasks = data.timerList
        }else if (data.msg.code) {
          toastr.warning("获取周期任务列表失败："+data.msg.code, 'Warning');
        }else if (!data.timerList.length) {
          toastr.info('暂没有周期任务', '');
        }
      },function (data) {
        toastr.warning("获取周期任务列表失败："+data.msg.code, 'Warning');
      });
    }
    $scope.getTaskList()

    $scope.moveUp = function(group) {
      angular.forEach($scope.task.groups, function(grp, key) {
        if (angular.equals(grp, group)) {
          if (key == 0) {
            return;
          } else {
            $scope.task.groups.splice(key, 1)
            $scope.task.groups.splice(key - 1, 0, group)
          }
          // $scope.task.groups.splice(key, 1)
        };
      });
      // alert('moveUp')
    }
    $scope.moveDown = function(group) {
      angular.forEach($scope.task.groups, function(grp, key) {
        if (angular.equals(grp, group)) {
          if (key == $scope.task.groups.length - 1) {
            return;
          } else {
            $scope.task.groups.splice(key, 1)
            $scope.task.groups.splice(key + 1, 0, group)
          }
          // $scope.task.groups.splice(key, 1)
        };
      });
      // alert('moveDown')
    }
    $scope.showGroup = function(group) {
      alert('showGroup')
    }
    $scope.deleteTask = function(task) {
      if (task instanceof Array) {
        angular.forEach(task, function(t) {
          angular.forEach($scope.tasks, function(tsk, key) {
            if (angular.equals(t, tsk)) {
              $scope.tasks.splice(key, 1);
              // console.log($scope.tasks);
            }
          })
        })
      } else {
        angular.forEach($scope.tasks, function(tsk, key) {
          if (angular.equals(tsk, task)) {
            $scope.tasks.splice(key, 1)
            // console.log($scope.tasks)
          };
        });
      }
      $scope.taskDialog.close('close')
    }
    $scope.taskSetSave = function(task) {
      // toastr.success('添加周期任务', '成功添加周期任务');
      // alert('taskSetSave')
      // $scope.closeThisDialog('close')
      // console.log($scope.task)
      // $scope.tasks.push(angular.copy($scope.task));
      // console.log($scope.tasks);
      // $scope.taskDialog.close('close')
      taskHelper.buildTask($scope.task).then(function(data) {
        // $scope.taskDialog
        if (data.statusText == 'OK') {
          toastr.success('', '成功添加周期任务');
          $scope.getTaskList()
          $scope.taskDialog.close('close')
        }
        // console.log(data);
      },function (data) {
        // console.log(data)
      });
    }
    $scope.saveTaskEdit = function() {
      $scope.taskDialog.close('close')
    }
    $scope.$watch('task.group', function(newval, oldval) {
      // console.log(newval)
      $scope.task.projects = [];
      angular.forEach(newval, function(group, index, array) {
        angular.forEach(group.projects, function(data, index, array) {
          $scope.task.projects.push(angular.copy(data));
        });
      });
      // console.log($scope.task.projects, '$scope.task.projects')
    }, true)

    //打开添加任务面板
    $scope.openPeriodicTaskSet = function(tsk, type) {
      $scope.dialogType = type;
      if (tsk) {
        $scope.task = tsk;
        $scope.dialogTitle = '修改周期任务';
      } else {
        $scope.task = {};
        $scope.dialogTitle = '添加周期任务';
        // console.log($scope.task);

      }
      $scope.taskDialog = ngDialog.open({
        template: 'src/task/tmpl/ng-dialog-task-set.html',
        className: 'ngdialog-theme-default',
        scope: $scope,
        width: '45%',
        // plain:true
        // ,
        // height:'800px'
        // ,
        //   preCloseCallback: function(value) {
        //     if (confirm('Are you sure you want to close without saving your changes?')) {
        //         return true;
        //     }
        //     return false;
        // }
        //
        //       preCloseCallback: function(value) {
        //     var nestedConfirmDialog = ngDialog.openConfirm({
        //         template:'\
        //             <p>Are you sure you want to close the parent dialog?</p>\
        //             <div class="ngdialog-buttons">\
        //                 <button type="button" class="ngdialog-button ngdialog-button-secondary" ng-click="closeThisDialog(0)">No</button>\
        //                 <button type="button" class="ngdialog-button ngdialog-button-primary" ng-click="confirm(1)">Yes</button>\
        //             </div>',
        //         plain: true
        //     });

        //     // NOTE: return the promise from openConfirm
        //     return nestedConfirmDialog;
        // }

      });
    }

//每天 每月 每小时全选
    $scope.onSelect = function ($item,$model) {
      // alert($item)
      // alert($model)
      // console.log($item,$model);
      if ($item.key==0) {
          switch($model){
            case -1:
              // setAll($scope.task.time.month,1,12);
              $scope.task.time.month=[]
              setAll($scope.task.time.month,1,12);
              // for (var i = 1; i < 13; i++) {
              //   $scope.task.time.month.push(i)
              // }
              break;          
            case -2:
              $scope.task.time.day=[]
              setAll($scope.task.time.day,0,6);
              break;
            case -3:
              $scope.task.time.date=[]
              setAll($scope.task.time.date,1,31);
              break;
            case -4:
              $scope.task.time.hour=[]
              setAll($scope.task.time.hour,0,23);
              break;
            default:
              return '';
          }
      }

      function setAll(target,from,to) {
        // target=[];
        for (var i = from; i <= to; i++) {
          target.push(i);
        }
        // console.log(target)
      }
    }
  }])
